<%--
  Created by IntelliJ IDEA.
  User: 34123
  Date: 2023/07/05 0005
  Time: 下午 04:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>出库</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="//unpkg.com/layui@2.8.6/dist/css/layui.css" rel="stylesheet">
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="//unpkg.com/layui@2.6.8/dist/css/layui.css">

    <!-- 引入 layui.js -->
    <script src="//unpkg.com/layui@2.6.8/dist/layui.js"></script>
    <style>
        .layui-form{
            display: flex;
            width: 100%;
        }
        #left{
            display: flex;
            flex-direction: column;
            /*margin-top: 60px;*/
            /*margin-left: 300px;*/
        }
        #right{
            display: flex;
            flex-direction: column;
            /*margin-top: 69px;*/
            margin-left: 250px;
        }
    </style>
</head>
<body>

<form action="#" id="fm">
    <div class="layui-form">
        <div id="left">
            <%--业务类型--%>
            <div class="layui-form-item">
                <div class="layui-input-group">
                    <div class="layui-input-split layui-input-prefix">
                        物料名
                    </div>
                    <input type="text" name="materialsName" placeholder="物料名称" class="layui-input">
                </div>
            </div><br>
            <%--项目名称--%>
            <div class="layui-form-item">
                <div class="layui-input-group">
                    <div class="layui-input-split layui-input-prefix">
                        供应商
                    </div>
                    <select name="supplier" lay-verify="">
                        <option value="" id="sup"></option>
                    </select>
                </div>
            </div><br>
            <%--相关客户--%>
            <div class="layui-form-item">
                <div class="layui-input-group">
                    <div class="layui-input-split layui-input-prefix">
                        库存
                    </div>
                    <input type="text"  name="productionCount" placeholder="库存" class="layui-input">
                </div>
            </div><br>
            <%--交易款号--%>
            <div class="layui-form-item">
                <div class="layui-input-group">
                    <div class="layui-input-split layui-input-prefix">
                        入库数量
                    </div>
                    <input type="text"name="materialInput" placeholder="出库数量" class="layui-input">
                </div>
            </div><br>
            <%--支出金额--%>
            <div class="layui-form-item">
                <div class="layui-input-group">
                    <div class="layui-input-split layui-input-prefix">
                        物料状态
                    </div>
                    <select name="materialsStatus" lay-verify="-1" id="materialsStatus">
                        <option value="-1">请选择物料</option>
                        <option value="1">面里料</option>
                        <option value="2">辅料</option>
                        <option value="3">包装辅料</option>
                    </select>
                </div>
            </div><br>
        </div>
        <div id="right">
            <%--支出日期--%>
            <div class="layui-form-item">
                <div class="layui-input-group">
                    <div class="layui-input-split layui-input-prefix">
                        入库时间日期
                    </div>
                    <input type="date" name="inputTime">
                </div>
            </div><br>
        </div>
    </div>
</form>
<button onclick="getAllisNull()" class="layui-btn layui-btn-warm" style="align-content: center">提交</button>
<script src="//unpkg.com/layui@2.8.6/dist/layui.js"></script>
<script>
    layui.use(function(){
        var element = layui.element;
        // 绑定自定义的 tab 元素
        element.tab({
            headerElem: '#tabHeader>.layui-btn',
            bodyElem: '#tabBody>div'
        });
    });
</script>
</body>
<script type="text/javascript">
    getAllMaterials()
    function getAllMaterials() {
        $.ajax({
            "url": "warehouse/getAllMaterials",
            "type": "get",
            "data": {},
            "dataType": "json",
            "success": function (result){
                console.log(result)
                let optemp = "";
                for (let i in result){
                    optemp += `
              <option value="\${result[i].supplier}">\${result[i].supplier}</option>
                `
                }
                $('#sup').after(optemp)
                layui.form.render('select'); // 重新渲染select元素
            },
            "error": function () {
                alert("出错了")
            }
        })
    }
</script>
<script type="text/javascript">
    function getAllisNull() {
        let materialsName = $('[name=materialsName]').val();
        let supplier = $('[name=supplier]').val();
        let productionCount = $('[name=productionCount]').val();
        let materialInput = $('[name=materialInput]').val();
        let materialsStatus = $('[name=materialsStatus]').val();
        let inputTime = $('[name=inputTime]').val();
        if (materialsName==null||materialsName==""){
            alert("物料名称不能为空")
            return false;
        }
        if (supplier==null||supplier==""){
            alert("供应商不能为空")
            return false;
        }
        if (productionCount==null||productionCount==""){
            alert("库存不能为空")
            return false;
        }
        if (materialInput==null||materialInput==""){
            alert("入库数量不能为空")
            return false;
        }
        if (materialsStatus==null||materialsStatus==""){
            alert("物料状态不能为空")
            return false;
        }
        if (inputTime==null||inputTime==""){
            alert("请选择入库时间")
            return false;
        }
        addExpend();
    }
    function addExpend(){
        let data = {};
        let value=$("#fm").serializeArray();
        console.log(value)
        $.each(value, function(index,item){
            data[item.name] = item.value;
        });
        let ino = JSON.stringify(data);
        console.log(ino)
        $.ajax( {
            "url"            :  "warehouse/addMaterials",
            "type"         :  "post",
            "data"         :  ino,
            "dataType" :  "json",
            "contentType":"application/json;charset=utf-8;",
            "success"  :  function(result) {
                layer.msg(result.message);
                $('#fm')[0].reset();
            },
            "error"       :  function() {
                alert("出错了")
            }
        } )
        $.ajax( {
            "url"            :  "warehouse/addMaterialStoreRelRu",
            "type"         :  "post",
            "data"         :  ino,
            "dataType" :  "json",
            "contentType":"application/json;charset=utf-8;",
            "success"  :  function(result) {
                layer.msg(result.message);
                $('#fm')[0].reset();
            },
            "error"       :  function() {
                alert("出错了")
            }
        } )
        $.ajax( {
            "url"            :  "warehouse/addStoreRu",
            "type"         :  "post",
            "data"         :  ino,
            "dataType" :  "json",
            "contentType":"application/json;charset=utf-8;",
            "success"  :  function(result) {
                layer.msg(result.message);
                $('#fm')[0].reset();
            },
            "error"       :  function() {
                alert("出错了")
            }
        } )
    }
</script>
</html>
